home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 81 / IOPROG_81.ISO / tips / Java / Eseguire operazioni in background / BackgroundJobExecutor.java < prev    next >
Encoding:
Java Source  |  2004-04-28  |  2.0 KB  |  95 lines

  1. import java.util.*;
  2.  
  3. public class BackgroundJobExecutor implements Runnable {
  4.     private Queue queue;
  5.     private boolean stopRequest = false;
  6.     private boolean stopping = false;
  7.     private Thread thread;
  8.     private final AbstractJob nop = new AbstractJob() {
  9.         public void execute() {
  10.             dispose();
  11.         }
  12.     };
  13.     public BackgroundJobExecutor() {
  14.         queue = new Queue();
  15.         thread = new Thread(this);
  16.     }
  17.     /**
  18.      * Inizia a processare i job
  19.      */
  20.     public void start() {
  21.         thread.start();
  22.     }
  23.     /**
  24.      * Aggiunge un job alla coda
  25.      */
  26.     public boolean add(AbstractJob job) {
  27.         if (job == null) {
  28.             throw new NullPointerException("Job cannot be null");
  29.         }
  30.         if (stopRequest || stopping) {
  31.             throw new IllegalArgumentException("BackgroundJobExecutor has been stopped.");
  32.         }
  33.         return queue.put(job);
  34.     }
  35.     public void run() {
  36.         while (!stopRequest) {
  37.             AbstractJob l = (AbstractJob)queue.get();
  38.             if (l == null) {
  39.                 break;
  40.             }
  41.             l.execute();
  42.         }
  43.         dispose();
  44.     }
  45.     /**
  46.      * Termina i job in coda e si ferma
  47.      */
  48.     public void stop() {
  49.         if (stopRequest) {
  50.             throw new IllegalArgumentException("BackgroundJobExecutor has been stopped.");
  51.         }
  52.         if (!stopping) {
  53.             stopping = true;
  54.             queue.putLast(nop);
  55.         }
  56.     }
  57.     /**
  58.      * Ferma l'esecusione senza necessariamente aver terminati i job in coda
  59.      */
  60.     public void dispose() {
  61.         stopRequest = true;
  62.         queue.clear();
  63.         if (thread != null) {
  64.             thread.interrupt();
  65.             thread = null;
  66.         }
  67.     }
  68.     private class Queue {
  69.         private List list = new LinkedList();
  70.         public synchronized Object get() {
  71.             while (list.size() == 0) {
  72.                 try {
  73.                     wait();
  74.                 } catch (InterruptedException ex) {
  75.                     return null;
  76.                 }
  77.             }
  78.             Object o = list.iterator().next();
  79.             list.remove(o);
  80.             return o;
  81.         }
  82.         public synchronized boolean put(Object obj) {
  83.             boolean b = list.add(obj);
  84.             notify();
  85.             return b;
  86.         }
  87.         public synchronized void clear() {
  88.             list.clear();
  89.         }
  90.         public synchronized void putLast(Object o) {
  91.             list.add(list.size(), o);
  92.         }
  93.     }
  94. }
  95.